package com.da.landlord;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.espirit.eap.sa.LoginUser;
import com.espirit.eap.sa.LoginUserService;
import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.exception.ApplicationException;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.security.SafeManager;
import com.googlecode.cswish.struts.FreemarkerResult;
import com.googlecode.cswish.struts.ServletRedirectResult;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Result;
import com.ra.landlord.LandlordOwnerVIPUser;
import com.ra.landlord.LandlordUserService;
import com.ra.landlord.TenantUser;
import com.ra.landlord.UserExtContext;
import com.ra.util.SmsService;

@Service
public class DaLandlordUserService {

	@Resource
	private GenericService genericService;
	
	@Resource
	private LoginUserService loginUserService;
	
	@Resource
	private SafeManager safeManager;
	
	@Resource
	private ObjectFactory objectFactory;
	
	@Resource
	private LandlordUserService landlordUserService;
	
	private static final String INVALID_CODE = "420";
	
	@Resource
	private SmsService smsService;
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public void _login(String productLogin, String productLoginDesc, String code) {
		
	}
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public Result login(String name, String password, boolean isEncrypted, String site) {
		if (StringUtils.isEmpty(name)) {
			throw new ApplicationException("请输入用户名");
		}
		
		String indexPage = landlordUserService.login(name, password, isEncrypted, site);
		LoginUser loginUser = (LoginUser) safeManager.getLoginUser(ServletActionContext.getRequest());
		if (((UserExtContext)loginUser.getUserEx()).loadLandlordUser() == null) {
			throw new ApplicationException("无效的房东");
		}
		
		Result result = new ServletRedirectResult("/");
		objectFactory.autoWireBean(result);
		return result;
	}
	
	public boolean cleanLastVisitPath() {
		HttpServletRequest request = ServletActionContext.getRequest();
		safeManager.setLastVisitInfo(request, null);
		return true;
	}
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public void _register(LandlordOwnerVIPUser model) {
	}
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public Result register(LandlordOwnerVIPUser model, String verifyCode, double longitude, double latitude) {
		landlordUserService.register(model, verifyCode, longitude, latitude);
		
		Result result = new ServletRedirectResult("/da/landlord/frame.index.html");
		objectFactory.autoWireBean(result);
		return result;
	}
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public void _forgetPassword(LandlordOwnerVIPUser model){
		
	}
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public Result forgetPassword(LandlordOwnerVIPUser model, String verifyCode){
		landlordUserService.resetPassword(model, verifyCode,model.getPassword());
		Result result = new ServletRedirectResult("/da/landlord/frame.index.html");
		objectFactory.autoWireBean(result);
		return result;
	}
	
	
	
}